/*
 * StepItem
 * 
 * versvion 1.0
 * 
 * 14.05.2009
 * 
 * Copyright (c) StrongQA, Inc.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of StrongQA, Inc.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with StronQA.Inc.
 *
 */

package reporting;

import static util.ConstantCollection.*;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


/**
 * @author Roman I.
 *
 */
class TCStep {
		
	/**
	 * id identifier
	 */
	private String id;
	
	/**
	 * name of actions, title of message
	 */
	private String name;
	
	/**
	 * time when message created 
	 */
	private Date time = new Date();
	
	/**
	 * status, general for one message, total for all sub-statuses
	 */
	private Integer status;
	
	/**
	 * collection of items (actions) 
	 */
	private List<StepItem> itemCollection = new ArrayList<StepItem>();
	
	/**
	 * number of action's step 
	 */
	private Integer lastStepNum = 0;
	
	
	/**
	 * Keeps essence of test case's step, equal one method marked @Test
	 * @param id identifier
	 * @param name of actions, title of message
	 */
	public TCStep(String id, String name) {
		this.id = id;
		this.name = name;
		status = STATUS_UNCHECKED;
	}
	
	/**
	 * @param id identifier
	 */
	public void setId(String id) {
		if (this.id == null ) {
			this.id = id;
		} else {
			System.out.println("The id was setted alrealy!");
		}
	}

	/**
	 * @return id identifier
	 */
	public String getId() {
		return id;
	}
	
	/**
	 * @return name of actions, title of message
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * @return time when message created
	 */
	public Date getTime() {
		return time;
	}
	
	/**
	 * @return status, general for one message, total for all sub-statuses
	 */
	public int getStatus() {
		return status;
	}
	
	/**
	 * @return Collection of items (actions)
	 */
	public List<StepItem> getItemCollection() {
		return itemCollection;
	}
		
	/**
	 * Add element to collection, check type, calculate id,
	 * stepNum equal lastId, and, if it is action, change lastStepNum.
	 * @param type, can be checkpoint, action or null
	 * @param description of action sub-step
	 * @param screenshot of error
	 * @param stepnum number of action's step
	 */
	public void addToCollection(String type, String description) {
		StepItem stepItem = null;
		
		if (type == "cp") {
			stepItem = new StepItem(type, description, "");
			itemCollection.add(stepItem);
		} else if (type == "action") {
			Integer stepNum = this.lastStepNum + 1;
			stepItem = new StepItem(type, description, stepNum.toString());
			itemCollection.add(stepItem);
			lastStepNum = lastStepNum + 1;
		} else {
			System.out.println("Incorrect StepItem type ('" + type + "')");
		}
	}

	/**
	 * Find object in collection (with lastId), and sets status to it, 
	 * and error's description. 
	 * @param status of action step, failed or passed
	 * @param errorDescription text description of error
	 */
	public void setItemStatus(int status, String errorDescription, StackTraceElement[] errorStack) {
		StepItem stepItem = itemCollection.get(itemCollection.size() - 1);
	
		if (status >=0 && status <= 7) {
			stepItem.setStatus(status);
			
			//Set 'status' property
			if (this.status < status) {
				this.status = status;
			}
			
			if (status >=4) {
				stepItem.setErrorDescription(errorDescription);
				stepItem.setErrorStack(errorStack);
			}
		}
	}
	
	/**
	 * Find object in collection (with lastId), and sets in screenshot's
	 * property path to the folder with pictures (create in ReportEngineNew)
	 * @param path to the screenshot
	 */
	public void setScrShot(String path) {
		StepItem stepItem = itemCollection.get(itemCollection.size() - 1);
		stepItem.setScreenshot(path);
	}
	
	public void setTrace(String path) {
		StepItem stepItem = itemCollection.get(itemCollection.size() - 1);
		stepItem.setPathToCommonTrace(path);
	}
	
	
}